package com.hymjweb.demo.hymj.system_maintenance.user_role.mapper;


import com.hymjweb.demo.framework.util.DataTypeUtil;
import com.hymjweb.demo.hymj.system_maintenance.role.mapper.RoleMapper;
import com.hymjweb.demo.hymj.system_maintenance.user_role.bean.UserRolePojo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.stereotype.Component;

import javax.xml.crypto.Data;
import java.util.List;
import java.util.Map;

@Mapper
@Component
public interface UserRoleMapper {



    class UserRoleMapperProvider{

        public String queryUserListByRoleId(Map param){
            String sql = new SQL() {{
                SELECT("t.*");
                FROM("xt_user t ");
                if(param!=null){
                    String roleId = DataTypeUtil.toStringObject(param.get("roleId"));
                    if(roleId!=null){
                        WHERE("t.id in (select t.user_id from sys_user_role t where t.role_id='" + roleId + "')" );
                    }
                }
            }}.toString();
            return  sql;
        }

        public String queryUnselectedRoleUser(Map param){
            // {roleId:roleIdValue,excludeUserIds:excludeUserIdsValue,keyUserName:keyUserNameValue}
            String sql = new SQL() {{
                SELECT("t.*");
                FROM("xt_user t ");
                WHERE("1=1 ");
            }}.toString();
            if(param!=null){
                String roleId = DataTypeUtil.toStringObject(param.get("roleId"));
                String excludeUserIds = DataTypeUtil.toStringObject(param.get("excludeUserIds"));
                String keyUserName = DataTypeUtil.toStringObject(param.get("keyUserName"));
                if(roleId!=null){
                    sql += "and t.id not in (select t.user_id from sys_user_role t where t.role_id='" + roleId + "') ";
                }
                if(excludeUserIds!=null){
                    sql += "and t.id not in (" + excludeUserIds + ") ";
                }
                if(keyUserName!=null){
                    sql += "and (t.name like " + DataTypeUtil.quoteAllLikeStr(keyUserName) + " or t.mobile like " + DataTypeUtil.quoteAllLikeStr(keyUserName) + ")" ;
                }
            }
            return  sql;
        }

        public String querySelectedRoleUser(Map param){
            // {roleId:roleIdValue,includeUserIds:includeUserIdsValue}
            String sql = new SQL() {{
                SELECT("t.*");
                FROM("xt_user t ");
                WHERE("1=1 ");
            }}.toString();
            if(param!=null){
                String roleId = DataTypeUtil.toStringObject(param.get("roleId"));
                String includeUserIds = DataTypeUtil.toStringObject(param.get("includeUserIds"));
                if(roleId!=null){
                    sql += "and t.id not in (select t.user_id from sys_user_role t where t.role_id='" + roleId + "') ";
                }
                if(includeUserIds!=null){
                    sql += "and t.id in (" + includeUserIds + ") ";
                }
            }
            return  sql;
        }

        public String checkUnique(UserRolePojo userRolePojo){
            String sql = new SQL() {{
                SELECT("t.*");
                FROM("sys_user_role t ");
                WHERE("1=1 and t.user_id = #{userId} and t.role_id = #{roleId}");
            }}.toString();
            return sql;
        }


        public String queryUserRoleByUserId(String userId){
            String sql = new SQL() {{
                SELECT("t.*");
                FROM("sys_user_role t ");
                WHERE("1=1 and t.user_id = #{userId}");
            }}.toString();
            return sql;
        }


        public String deleteByRoleIdAndUserId(String roleId, String roleUserId){
            String sql = new SQL() {{
                DELETE_FROM("sys_user_role t");
                WHERE("t.user_id = #{roleUserId} and t.role_id = #{roleId}");
            }}.toString();
            return sql;
        }

    }

    @ResultType(Map.class)
    @SelectProvider(type = UserRoleMapperProvider.class, method = "queryUserListByRoleId")
    List<Map<String, Object>> queryUserListByRoleId(Map param);


    @ResultType(Map.class)
    @SelectProvider(type = UserRoleMapperProvider.class, method = "queryUnselectedRoleUser")
    List<Map<String, Object>> queryUnselectedRoleUser(Map param);

    @ResultType(Map.class)
    @SelectProvider(type = UserRoleMapperProvider.class, method = "querySelectedRoleUser")
    List<Map<String, Object>> querySelectedRoleUser(Map param);

    /**
     * create table sys_user_role
     * (
     * 	id varchar(20) not null comment '系统用户角色表ID'
     * 		primary key,
     * 	user_id varchar(20) null comment '用户ID,关联xt_user.id',
     * 	role_id varchar(20) null comment '角色ID,关联sys_role.id'
     * )
     * comment '系统用户角色表';
     * @param userRolePojo
     * @return
     */
    @Results(id = "userRoleMapId",value = {
            @Result(column = "id", property = "id"),
            @Result(column = "user_id", property = "userId"),
            @Result(column = "role_id", property = "roleId")
    })
    @SelectProvider(type = UserRoleMapperProvider.class, method = "checkUnique")
    UserRolePojo checkUnique(UserRolePojo userRolePojo);



    @ResultMap("userRoleMapId")
    @SelectProvider(type = UserRoleMapperProvider.class, method = "queryUserRoleByUserId")
    List<UserRolePojo> queryUserRoleByUserId(String userId);


    /**
     * 批量插入用户角色
     * @param userRolePojoList
     * @return
     */
    @Insert(
            "<script>" +
                    "insert into sys_user_role(id, user_id, role_id) values " +
                    "<foreach collection='userRolePojoList' item='pojo' separator=','>" +
                    "(#{pojo.id},#{pojo.userId},#{pojo.roleId})" +
                    "</foreach>" +
            "</script>"
    )
    @Options(keyProperty = "id")
    int batchInsertUserRole(@Param("userRolePojoList") List<UserRolePojo> userRolePojoList);


    @DeleteProvider(type = UserRoleMapperProvider.class, method = "deleteByRoleIdAndUserId")
    int deleteByRoleIdAndUserId(String roleId, String roleUserId);

}
